package com.atguigu.guli.service.ucenter.controller.api;

import com.atguigu.guli.common.util.utils.HttpClientUtils;
import com.atguigu.guli.service.base.consts.Keys;
import com.atguigu.guli.service.base.handler.JwtHelper;
import com.atguigu.guli.service.base.handler.JwtInfo;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.mapper.MemberMapper;
import com.atguigu.guli.service.ucenter.properties.WxLoginProperties;
import com.atguigu.guli.service.ucenter.service.WxService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.UUID;


@Controller  //因为获取wx二维码的接口需要返回重定向
@RequestMapping("/api/ucenter/wx")
public class ApiWxLoginController {
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    WxService wxService;
    @Autowired
    WxLoginProperties wxLoginProperties;
    @ResponseBody
    @GetMapping("get")
    public R get(HttpSession session){
        Object seeKey = session.getAttribute("seeKey");
        System.out.println("seeKey: " + seeKey);
        return R.ok();
    }
    @ResponseBody
    @GetMapping("set")
    public R set(HttpSession session){
        session.setAttribute("seeKey" , "seeVal");
        return R.ok();
    }

    //2、回调接口：wx用户授权后的回调： 获取wx用户的数据转为jwt，响应
    @GetMapping("callback")
    public String wxLoginCallback(String code, String state , HttpSession session){
       String redirectUrl = wxService.wxLoginCallback(code , state , session);
       return redirectUrl;
    }

    /*
        springmvc接口的返回值：
            1、返回视图名，当做转发视图对象
            2、返回重定向路径：
            3、返回字符串：
     */
    //1、获取wx登录二维码
    @GetMapping("login")
    public String  wxLogin(HttpSession session){
       String redirectUrl = wxService.wxLogin(session);
       return redirectUrl;
    }

}
